<?php
session_start();

include_once('config.php');
include_once('saetv2.ex.class.php');

$o = new SaeTOAuthV2(SWB_AKEY , SWB_SKEY);

if (isset($_REQUEST['code']))
{
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = SWB_RequestToken;
	try
	{
		$token = $o->getAccessToken('code', $keys) ;
	}
	catch (OAuthException $e)
	{
	}
}

if ($token)
{
	$_SESSION['token'] = $token;
	setcookie('weibojs_' . $o->client_id, http_build_query($token));

	$c = new SaeTClientV2(SWB_AKEY , SWB_SKEY , $_SESSION['token']['access_token']);
	$uid_get = $c->get_uid();
	$uid = $uid_get['uid'];
	$user_message = $c->show_user_by_id($uid); //根据ID获取用户等基本信息

	if ($user_message['id'] > 0) // 使用后不能在修改下面参数否则出错
	{
		$param = array('action' => "http://" . URL . "/user/register_api.php", 'orther_web_id' => $user_message['id'], 'orther_web_name' => $user_message['name'], 'orther_web' => '新浪微博');
		echo form($param);
		exit;
	}
	else
	{
		if ($user_message['error_code'])
		{
			if ($_SESSION["duoduouser"] == 1)
			{
				echo "错误代码：".$user_message['error_code'] . ":" . $user_message['error'];
				echo "<br>";
				echo "：<a href=\"http://bbs.duoduo123.com/read-htm-tid-80727-ds-1.html\">帮助</a>";
			}
			else
			{
				header("Location:http://" . URL."/");
			}
		}
		exit;
	}
}

?>